/**
 * 业务功能视图
 */
//模型文件版本数据
var functionpointVersioninfoStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({	//proxy从哪里获取数据
		url : 'showFunctionpointVersionInfo.action?method=showFunctionpointVersionInfo',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({	//reader如何解析数据
		root : 'versionInfo',
		totalProperty : 'totalVersionInfoSize',
		id : 'functionpointversion'
	}, [ {
		name : 'groupId'
	}, {
		name : 'time'
	},{
		name : 'author'
	}, {
		name : 'versionId'
	}
	])
});
//版本表格列模型
var functionpointVersioninfosm=new Ext.grid.CheckboxSelectionModel ({singleSelect : false});
var versioninfoColumnModel=new Ext.grid.ColumnModel(
		[functionpointVersioninfosm,
		 new Ext.grid.RowNumberer(),
		 {
			header : '版本号',
			width : 100,
			dataIndex : 'versionId',
			sortable : true,
			align : 'center'
		 },   {
			 header : '修改日期',
			 width : 200,
			 dataIndex : 'time',
			 sortable : true,
             align : 'center'
		 },{
			 header : '作者',
			 width : 100,
			 dataIndex : 'author',
			 sortable : true,
			 align : 'center'
         },{
			 header : '模型种类',
			 width : 100,
			 dataIndex : 'groupId',
			 sortable : true,
			 align : 'center'
         }]
          );
//模型版本信息列表
var versioninfoList=new Ext.grid.EditorGridPanel({
	id:'fucntionversion',
	title: "版本历史",
	region:'center',
	ds : functionpointVersioninfoStore ,	//绑定store
	enableColumnResize : true,
	border : true,
	//clicksToEdit:1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	bodyStyle:'width:100%',
    autoWidth:true,
    editable:true,
    sm : functionpointVersioninfosm,
    bbar: new Ext.PagingToolbar({//下面的分页条
        pageSize: 15,
        store: functionpointVersioninfoStore ,
        displayInfo: true,
        displayMsg: '当前显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: "没有记录",
        lastText:"尾页",   
        nextText :"下一页",   
        prevText :"上一页",   
        firstText :"首页",   
        refreshText:"刷新页面"        
    }),
	cm : versioninfoColumnModel,
	store : functionpointVersioninfoStore 
});
var versioninfoTabPanel = new Ext.TabPanel({
	border : true,
	region:'south',
	autoScroll:true,
	//clicksToEdit:1,
	//autoHeight:true,
    //autoWidth:true,
	height:200,
	items:[ versioninfoList,
	{
	title: "功能点具体信息",
	region:'center',
	
	collapsible : true,
	border:false,
	contentEl:'functionPointDetail'
	}]
	,
	activeItem: 1
	});

var functionpoint = new Ext.Panel({
	id : 'functionpoint-panel',
	title : '业务功能视图',
	layout : 'border',
	//closable:true,
	defaults : {
		
		bodyStyle : 'padding:15px'
	},
	items : [ 
	         versioninfoTabPanel,
	         {
	    region : 'center',
		border : false,
		

		autoScroll:true,
		contentEl : 'functionCanvas'

	} ]
});
function returnToProcess(){
	if (pid=='-1'&& pText=='-1') {
		Ext.getCmp('content-panel').layout.setActiveItem('function-list-panel');
		  showFunctionList();
	}
	else {  
	  Ext.getCmp('content-panel').layout.setActiveItem('process-panel');
	  showProcessContent(pid,pText);}
}
var parameterStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showParameter.action?method=showParameter',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "parameter",
		totalProperty : "totalParameter",
		id:"id"
    }, [ {
		name : 'id'
	}, {
		name : 'enName'
	},{
		name : 'cnName'
	},{
		name : 'description'
	},{
		name : 'inOutType'
	}, {
		name : 'isBuiltIn'
	}, {
		name : 'paramTypeId'
	}, {
		name : 'paramTypeName'
	}, {
		name : 'functionPointId'
	}, {
		name : 'collectionType'
	}, {
		name : 'restrictions'
	}, {
		name : 'fileId'
	}])
});
var pid;
var pText;



function showFunctionContent(cid,text,processid,processText) {    
    pid=processid;
    pText=processText;
    
    functionpointVersioninfoStore.load({
    	params:{
    		cid:cid
    	},
    	callback: function(r, options, success) { 
    		if (success == false) { 
    		//alert("加载functionpoint-version数据失败，无对应数据或者系统出现异常！"); 
    		}
    		else{
    		//alert("加载domain-version数据成功！");	
    		}
    	}
    });
    
    parameterStore.load({ 
    	params : {
			cid : cid
		},
		callback: function(r, options, success) { 
		if (success == false) {  
		alert("加载数据失败，无对应数据或者系统出现异常！"); 
		}else{ 
			var canvas=document.getElementById('functionCanvas');
			var ctx=canvas.getContext('2d'); Ext.getDom('functionPointDetail').innerHTML = "";
			 drawFunctionModel(300,150,ctx,parameterStore,text);
			
			 var cm=new Ext.grid.ColumnModel([
				              					new Ext.grid.RowNumberer(),
				              					{header:'编号',dataIndex:'id'},
				              					{header:'名称(中文)',dataIndex:'cnName'},
				              					{header:'名称(英文)',dataIndex:'enName'},
				              					{header:'备注',dataIndex:'description'},
				              					{header:'输入输出',dataIndex:'inOutType'},
				              					{header:'内建类型',dataIndex:'isBuiltIn'},
				              					{header:'参数类型Id',dataIndex:'paramTypeId'},
				              					{header:'参数类型',dataIndex:'paramTypeName'},
				              					{header:'功能点Id',dataIndex:'functionPointId'},
				              					{header:'集合类型',dataIndex:'collectionType'},
				              					{header:'约束',dataIndex:'restrictions'},
				              					{header:'文件Id',dataIndex:'fileId'}
				              				]);
				              			var grid=new Ext.grid.GridPanel({
				              					renderTo:'functionPointDetail',
				              					 viewConfig: {
				              			            forceFit:true
				              			        },
				              					layout:'fit',
				              					stripeRows: true,
				              					autoHeight:true,
				              					loadMask:true,
				              					store:parameterStore,
				              					cm:cm
//				              					,			              						
//				              					listeners : {
//				              						cellclick : {
//				              						fn : function(grid, rowIndex, columnIndex, e) {
//				              							//点击记录，在表单中显示记录信息
//				              							selected_row = rowIndex;
//				              						var record = grid.getStore().getAt(rowIndex);
//				              						//var fAmount=grid.getCount();
//				              						
//				              						
//				              				       		              				    
//				              				      	 Ext.getCmp('content-panel').layout.setActiveItem('functionpoint-panel');
//				              				      	 showFunctionContent(functionStore.getAt(rowIndex).get('id'),functionStore.getAt(rowIndex).get('cnName'),processid,pText);
//				              				       
//				              				      
//				              				   
//				              						},
//				              						scope : this
//				              					}
//				              				}
				              				});
			}
		}
		});

		
	  
}
function drawFunctionModel(startx,starty,ctx,parameterStore,text){
	 ctx.restore();
	 ctx.clearRect(0, 0, 800, 800);
	 ctx.save();
	 ctx.translate(startx, starty);
	 var pAmount=parameterStore.getCount();
	 var length=(pAmount+1)*20; //function视图长度
	 
     var gradient = ctx.createLinearGradient(0, 0, 0, length);
	 gradient.addColorStop(0, 'rgb(154, 205, 50)');
	 gradient.addColorStop(1, 'rgb(255,255,240)');
	 ctx.fillStyle = gradient;
	 ctx.fillRect( 0, 0, 200, length); //渐变的背景色
	 
	 ctx.strokeStyle = '#000000'; 
	 ctx.beginPath(); 
	 ctx.rect(0.5,0.5, 200, length);
	 ctx.stroke();              //黑边框
	 
	 var functionImage=new Image();
	 functionImage.onload=function(){
	 ctx.drawImage(functionImage,3,2,16,16);
	 };
	 functionImage.src='images/modelUnit/function/function_name.gif';
	 
	 ctx.fillStyle ="black";
	 ctx.font = "10pt Arial";
	 ctx.fillText(text,25,16,175); //标题栏
	 
	 
	 
	 ctx.beginPath();
	 ctx.moveTo(0,20);
	 ctx.lineTo(200,20);
	 ctx.stroke();   
	 ctx.beginPath();
	 ctx.moveTo(0.5,20.5);
	 ctx.lineTo(200.5,20.5);
	 ctx.stroke();        //画分割线
	 
	 

	 var inputImg=new Image();
	 inputImg.onload=function(){
		 var outputImg=new Image();
		 outputImg.onload=function(){
			 var inoutputImg=new Image();
			 inoutputImg.onload=function(){
     for (var i=0;i<pAmount;i++){
    	 var record=parameterStore.getAt(i);	
    	 if (record.get('inOutType')=='input')
			ctx.drawImage(inputImg,3,22+i*20,16,16);
    	 else if (record.get('inOutType')=='output')
 			ctx.drawImage(outputImg,3,22+i*20,16,16);
    	 else ctx.drawImage(inoutputImg,3,22+i*20,16,16);
			ctx.fillStyle ="black";
			ctx.font = "10pt Arial";
			ctx.fillText('['+record.get('cnName')+']'+record.get('enName'),22,36+i*20,175); //参数栏
		 }
			 };
	 inoutputImg.src='images/modelUnit/function/inoutput.gif';
	 };
	 outputImg.src='images/modelUnit/function/output.gif';
	 };
	 inputImg.src='images/modelUnit/function/input.gif';
	 
	
	 var parameterImage=new Image();
		 parameterImage.onload=function(){
			 var parameterInfo=new Image();
	  		 parameterInfo.onload=function(){
	 for (var i=0;i<pAmount;i++)
	 {
		 x=parseInt(-250*Math.cos(i*Math.PI*2/pAmount));
  	     if (x>200) x=1.1*x;
  	     y=parseInt(-250*Math.sin(i*Math.PI*2/pAmount));
  	     gradient = ctx.createLinearGradient(x, y, 0, 100);
  		 gradient.addColorStop(0, 'rgb(230, 230, 250)');
  		 gradient.addColorStop(1, 'rgb(255,255,240)');
  		 ctx.fillStyle = gradient;
  		 ctx.fillRect( x, y, 200, 100); //渐变的背景色
  		
  		 ctx.lineWidth = 1;
  		 ctx.strokeStyle = '#000000'; 
  		 ctx.beginPath(); 
  		
  		 ctx.rect(x-0.5,y-0.5, 200, 100);
  		 ctx.stroke();              //黑边框
  		 
  		 
  		
  		 ctx.drawImage(parameterImage,x+3,y+2,16,16);
  		
  		 ctx.fillStyle ="black";
  		 ctx.font = "10pt Arial";
  		 ctx.fillText(parameterStore.getAt(i).get('cnName'),x+25,y+16,175); //标题栏
  		 
  		 ctx.beginPath();
  		 ctx.moveTo(x,y+20);
  		 ctx.lineTo(x+200,y+20);
  		 ctx.stroke();
  		 
  		 ctx.beginPath();
  		 ctx.moveTo(x+0.5,y+20.5);
  		 ctx.lineTo(x+200.5,y+20.5);
  		 ctx.stroke();        //画分割线
  		 
  		
  			 for (var j=0;j<4;j++){
  				 ctx.drawImage(parameterInfo,x+3,y+22+j*20,16,16);
  			 }
  		
		 ctx.fillStyle ="black";
  		 ctx.font = "10pt Arial";
		 ctx.fillText('输入输出：'+parameterStore.getAt(i).get('inOutType'),x+22,y+36,175);
		 ctx.fillText('内建类型：'+parameterStore.getAt(i).get('isBuiltIn'),x+22,y+56,175);
		 ctx.fillText('参数类型：'+parameterStore.getAt(i).get('paramTypeName'),x+22,y+76,175);
		 ctx.fillText('集合类型：'+parameterStore.getAt(i).get('collectionType'),x+22,y+96,175);
		 drawLine(x+100,y+50,100,length/2,ctx,200,100);
	 	 }
	  		 };
			 parameterInfo.src='images/modelUnit/function/parameter_info.gif';
		 };
  		 parameterImage.src='images/modelUnit/function/parameter_name.gif';
	
}
